import { InjectionKey } from 'vue'
import { createStore, Store,useStore as baseUseStore } from 'vuex'
import {PageData,UserInfo} from './types'

import config from '../project.config'
export const store = createStore({
  state:{
		token:null,
		pageData:null,
		userInfo:null
	},
  mutations: {
		setPageData(state:any,data:PageData){
			data.commodity.forEach((list:any)=>{
				if(list.images){
					list.images=list.images.map((item:string):string=>{
						var url=item.indexOf('http')!=-1?item:config.serverAddress+item;
						return url;
					})
				}
			});
			state.pageData=data;
		},
		setUserInfo(state:any,data:UserInfo){
			localStorage.setItem('userInfo',JSON.stringify(data));
			state.userInfo=data
		},
		setToken(state:any,token:string){
			localStorage.setItem('token',token)
			state.token=token;
		}
  },
	getters:{
		getToken(state:any){
			var token:string=state.token||localStorage.getItem('token')
			return token;
		},
		getuserInfo(state:any){
			var userinfo=state.userInfo || JSON.parse(localStorage.getItem('userInfo')||'{}')
			return userinfo
		},
		getPageData(state:any){
			return state.pageData
		}
	}
})
 
